﻿@{
    string from = "";
    from = Request.Form["from"];

    switch(from)
    {
        case "servers":
        {
            string selectQueryString = "SELECT name FROM servers";

            string name, domain_name_servers, default_lease_time, max_lease_time, authoritative, log_facility;

            name = Request.Form["name"];
            domain_name_servers = Request.Form["domain_name_servers"];
            default_lease_time = Request.Form["default_lease_time"];
            max_lease_time = Request.Form["max_lease_time"];
            authoritative = Request.Form["authoritative"];
            log_facility = Request.Form["log_facility"];

            string[] tab = new string[]{name, domain_name_servers, default_lease_time, max_lease_time, authoritative, log_facility};
            bool first = false;

            for(int i=0; i<tab.Length; i++)
            {
                if(tab[i]!="")
                {
                    if(!first)
                    {
                        switch(i)
                        {
                            case 0:
                            {
                                selectQueryString += " WHERE name='"+name+"'";
                                break;
                            }
                            case 1:
                            {
                                selectQueryString += " WHERE domain_name_servers='"+domain_name_servers+"'";
                                break;
                            }
                            case 2:
                            {
                                selectQueryString += " WHERE default_lease_time='"+default_lease_time+"'";
                                break;
                            }
                            case 3:
                            {
                                selectQueryString += " WHERE max_lease_time='"+max_lease_time+"'";
                                break;
                            }
                            case 4:
                            {
                                selectQueryString += " WHERE authoritative='"+authoritative+"'";
                                break;
                            }
                            case 5:
                            {
                                selectQueryString += " WHERE log_facility='"+log_facility+"'";
                                break;
                            }
                        }
                        first = true;
                    }
                    else
                    {
                        switch(i)
                        {
                            case 0:
                            {
                                selectQueryString += " AND name='"+name+"'";
                                break;
                            }
                            case 1:
                            {
                                selectQueryString += " AND domain_name_servers='"+domain_name_servers+"'";
                                break;
                            }
                            case 2:
                            {
                                selectQueryString += " AND default_lease_time='"+default_lease_time+"'";
                                break;
                            }
                            case 3:
                            {
                                selectQueryString += " AND max_lease_time='"+max_lease_time+"'";
                                break;
                            }
                            case 4:
                            {
                                selectQueryString += " AND authoritative='"+authoritative+"'";
                                break;
                            }
                            case 5:
                            {
                                selectQueryString += " AND log_facility='"+log_facility+"'";
                                break;
                            }
                        }
                    }
                }
            }

            Response.Redirect("search.cshtml?sql="+selectQueryString+"&from=servers");
            break;
        }
        case "subnets":
        {
            string selectQueryString = "SELECT name FROM db.subnets";

            string name, address, netmask, broadcast_address, routers, server, building, _range;

            name = Request.Form["name"];
            address = Request.Form["address"];
            netmask = Request.Form["netmask"];
            broadcast_address = Request.Form["broadcast_address"];
            routers = Request.Form["routers"];
            server = Request.Form["server"];
            building = Request.Form["building"];
            _range = Request.Form["_range"];


            string[] tab = new string[]{name, address, netmask, broadcast_address, routers, server, building, _range};
            bool first = false;

            for(int i=0; i<tab.Length; i++)
            {
                if(tab[i]!="")
                {
                    if(!first)
                    {
                        switch(i)
                        {
                            case 0:
                            {
                                selectQueryString += " WHERE name='"+name+"'";
                                break;
                            }
                            case 1:
                            {
                                selectQueryString += " WHERE address='"+address+"'";
                                break;
                            }
                            case 2:
                            {
                                selectQueryString += " WHERE netmask='"+netmask+"'";
                                break;
                            }
                            case 3:
                            {
                                selectQueryString += " WHERE broadcast_address='"+broadcast_address+"'";
                                break;
                            }
                            case 4:
                            {
                                selectQueryString += " WHERE routers='"+routers+"'";
                                break;
                            }
                            case 5:
                            {
                                selectQueryString += " WHERE server='"+server+"'";
                                break;
                            }
                            case 6:
                            {
                                selectQueryString += " WHERE building='"+building+"'";
                                break;
                            }
                            case 7:
                            {
                                selectQueryString += " WHERE _range='"+_range+"'";
                                break;
                            }
                        }
                        first = true;
                    }
                    else
                    {
                        switch(i)
                        {
                            case 0:
                            {
                                selectQueryString += " AND name='"+name+"'";
                                break;
                            }
                            case 1:
                            {
                                selectQueryString += " AND address='"+address+"'";
                                break;
                            }
                            case 2:
                            {
                                selectQueryString += " AND netmask='"+netmask+"'";
                                break;
                            }
                            case 3:
                            {
                                selectQueryString += " AND broadcast_address='"+broadcast_address+"'";
                                break;
                            }
                            case 4:
                            {
                                selectQueryString += " AND routers='"+routers+"'";
                                break;
                            }
                            case 5:
                            {
                                selectQueryString += " AND server='"+server+"'";
                                break;
                            }
                            case 6:
                            {
                                selectQueryString += " AND building='"+building+"'";
                                break;
                            }
                            case 7:
                            {
                                selectQueryString += " AND _range='"+_range+"'";
                                break;
                            }
                        }
                    }
                }
            }

            Response.Redirect("search.cshtml?sql="+selectQueryString+"&from=subnets");
            break;
        }
        case "hosts":
        {
            string selectQueryString = "SELECT name FROM db.hosts";

            string name, hardware_ethernet, fixed_address, subnet;

            name = Request.Form["name"];
            hardware_ethernet = Request.Form["hardware_ethernet"];
            fixed_address = Request.Form["fixed_address"];
            subnet = Request.Form["subnet"];

            string[] tab = new string[]{name, hardware_ethernet, fixed_address, subnet};
            bool first = false;

            for(int i=0; i<tab.Length; i++)
            {
                if(tab[i]!="")
                {
                    if(!first)
                    {
                        switch(i)
                        {
                            case 0:
                            {
                                selectQueryString += " WHERE name='"+name+"'";
                                break;
                            }
                            case 1:
                            {
                                selectQueryString += " WHERE hardware_ethernet='"+hardware_ethernet+"'";
                                break;
                            }
                            case 2:
                            {
                                selectQueryString += " WHERE fixed_address='"+fixed_address+"'";
                                break;
                            }
                            case 3:
                            {
                                selectQueryString += " WHERE subnet='"+subnet+"'";
                                break;
                            }
                        }
                        first = true;
                    }
                    else
                    {
                        switch(i)
                        {
                            case 0:
                            {
                                selectQueryString += " AND name='"+name+"'";
                                break;
                            }
                            case 1:
                            {
                                selectQueryString += " AND hardware_ethernet='"+hardware_ethernet+"'";
                                break;
                            }
                            case 2:
                            {
                                selectQueryString += " AND fixed_address='"+fixed_address+"'";
                                break;
                            }
                            case 3:
                            {
                                selectQueryString += " AND subnet='"+subnet+"'";
                                break;
                            }
                        }
                    }
                }
            }

            Response.Redirect("search.cshtml?sql="+selectQueryString+"&from=hosts");
            break;
        }
    }
}